/**
 * 
 */
package com.antilia.infomaresme.pages.edit.news;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.IAjaxCallDecorator;
import org.apache.wicket.ajax.calldecorator.AjaxCallDecorator;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.odlabs.wiquery.plugins.ckeditor.CKeditorBehavior;

import com.antilia.infomaresme.EnumDropDownChoice;
import com.antilia.infomaresme.data.dataobjects.Item;
import com.antilia.infomaresme.data.dataobjects.ItemNews;
import com.antilia.infomaresme.data.dataobjects.VisibilityStatus;
import com.antilia.infomaresme.form.ShinyForm;
import com.antilia.infomaresme.pages.RoundSubmitLinkPanel;

/**
 * @author Ernesto
 *
 */
public abstract class AddEditNewsPanel extends Panel {

	private static final long serialVersionUID = 1L;

	private ItemNews news;
	
	private Form<ItemNews> form;
	
	private TextArea<String> description;
	/**
	 * @param id
	 */
	public AddEditNewsPanel(String id, IModel<Item> model, ItemNews news) {
		super(id);
		if(news != null) 
			this.news = news;
		else {
			this.news = new ItemNews();
			this.news.setStatus(VisibilityStatus.hidden);
			this.news.setItem(model.getObject());
		}
		form = new ShinyForm<ItemNews>("form", new CompoundPropertyModel<ItemNews>(news));
		TextField<String> title = new TextField<String>("title", new PropertyModel<String>(this.news, "title"));
		title.setRequired(true);
		form.add(title);
		EnumDropDownChoice<VisibilityStatus> status = new EnumDropDownChoice<VisibilityStatus>("status", VisibilityStatus.class);
		status.setDefaultModel(new PropertyModel<VisibilityStatus>(this.news, "status"));
		form.add(status);
		description = new TextArea<String>("description", new PropertyModel<String>(this.news, "text"));
		form.add(description);
		description.setOutputMarkupId(true);
		CKeditorBehavior behavior =new CKeditorBehavior();
		//behavior.getStyleSheet().setSkin("office2003");
		behavior.setResizeEnabled(false);
		behavior.setAutoUpdateElement(true);
		description.add(behavior);
		add(form);		
		form.add(new RoundSubmitLinkPanel("save", new ResourceModel("actions.save")) {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				target.add(form);
			}
			
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				onSave(target, form);
			}
			
			protected IAjaxCallDecorator getAjaxCallDecorator() {
				// TODO Auto-generated method stub
				return new AjaxCallDecorator() {

					private static final long serialVersionUID = 1L;
					
					@Override
					public CharSequence decorateScript(Component c, CharSequence script) {
						StringBuilder sb1 = new StringBuilder(100);
						sb1.append("document.getElementById('");
						sb1.append(description.getMarkupId());
						sb1.append("').value=CKEDITOR.instances.");
						sb1.append(description.getMarkupId());
						sb1.append(".getData();");			
						sb1.append(script);
						return sb1.toString();
					}
				};
			}
		});
	}
	
	protected abstract void onSave(AjaxRequestTarget target, Form<?> form);

	
	public ItemNews getNews() {
		return news;
	}

	public void setNews(ItemNews news) {
		this.news = news;
	}
}
